package com.image.androidimagebroswer.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.OnPhotoTapListener;
import com.github.chrisbanes.photoview.PhotoView;
import com.image.androidimagebroswer.R;

import java.util.List;

/**
 * Created by liuzhao.futrue on 2018/8/26.
 */

public class BigImageAdapter extends PagerAdapter {

    private Context mContext;
    private List<String> mPathList;

    public BigImageAdapter(Context context, List<String> list) {
        this.mContext = context;
        this.mPathList = list;
    }


    @Override
    public int getCount() {
        return mPathList == null ? 0 : mPathList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    /**
     * 创建条目：加载layout；数据绑定操作；
     *
     * @param container
     * @param position
     * @return
     */
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_bigimage, null);
        final PhotoView imageView = view.findViewById(R.id.iv_item_bigimage);
        imageView.setOnPhotoTapListener(new OnPhotoTapListener() {
            @Override
            public void onPhotoTap(ImageView view, float x, float y) {
                imageView.setRotationBy(90);// 每次旋转90度，setRotationBy是旋转到的意思
//                imageView.setZoomTransitionDuration(2000);// 图片放大的动画，在2s内完成
//                imageView.setZoomable(false);// 设置是否可以滑动，false为不可以；
            }
        });
        Glide.with(mContext).load(mPathList.get(position)).into(imageView);
        container.addView(view);
        return view;
    }

    /**
     * 销毁条目的回调
     *
     * @param container
     * @param position
     * @param object
     */
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}
